package cn.melon.view;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Point;
import java.awt.image.BufferedImage;

import javax.swing.JButton;
import javax.swing.JFrame;

import cn.melon.helper.BufferedImageHelper;
import cn.melon.view.control.ChangeButtonBacImg;
import cn.melon.view.control.OpenBrowser;

/**
 * @ClassName: JImgChangeButton
 * @author Hzy
 * 继承自JButton控件，当鼠标移到图片上时会改变图片。由于setRollOver的机制应该是不断在原图上重绘ImageIcon而不会清楚上次的绘画内容，故当有带阴影、半透明效果的图片时，不能使用setROlloverIcon
 */
public class JImgChangeButton extends JButton{
    private int width;
    private int height;
    private String bacImgPath;
    private String originImgPath;
    private String changedImgPath;
    private boolean paintChangedImg;
    private Point location;
    private JFrame root;
    public JImgChangeButton(int width,int height,String bacImgPath,String originImgPath,String changedImgPath,JFrame window){        
        super();
        this.width = width;
        this.height= height;
        this.originImgPath = originImgPath;
        this.changedImgPath = changedImgPath;
        this.bacImgPath = bacImgPath;
        this.root = window;
        this.location = new Point();
        this.paintChangedImg = false;
        
        this.setPreferredSize(new Dimension(width,height));
        this.setMargin(new Insets(0,0,0,0));
        this.setContentAreaFilled(false);
        this.setBorder(null);
        this.setFocusPainted(false);
        this.addMouseListener(new ChangeButtonBacImg());//鼠标移至上面改变icon
        this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }
    public JImgChangeButton(int width,int height,String bacImgPath,String originImgPath,String changedImgPath,String url,JFrame window){
        super();
        this.width = width;
        this.height= height;
        this.originImgPath = originImgPath;
        this.changedImgPath = changedImgPath;
//        this.location = new Point((this.getLocationOnScreen().x - root.getLocationOnScreen().x),(this.getLocationOnScreen().y-root.getLocationOnScreen().y));
        this.location = new Point();
        this.root = window;
        this.paintChangedImg = false;
        this.setPreferredSize(new Dimension(width,height));
        this.setContentAreaFilled(false);
        this.bacImgPath = bacImgPath;
        this.setBorder(null);
        this.setFocusPainted(false);
        this.addMouseListener(new ChangeButtonBacImg());//鼠标移至上面改变icon
        this.addActionListener(new OpenBrowser(url));//单击按钮打开对应网页
        this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }
    @Override
    public void paintComponent(Graphics g){
        Graphics2D g2d = (Graphics2D) g;
        BufferedImage img = new BufferedImage(width,height,BufferedImage.TYPE_4BYTE_ABGR);
        BufferedImage bacImg = new BufferedImage(width,height,BufferedImage.TYPE_4BYTE_ABGR);
        try{
//            bacImg = ImageIO.read(new File(bacImgPath));
            bacImg = BufferedImageHelper.getSource(this.getClass(), bacImgPath);

            if(paintChangedImg){
//                img = ImageIO.read(new File(changedImgPath));
                img =  BufferedImageHelper.getSource(this.getClass(), changedImgPath);
            }else{
//                img = ImageIO.read(new File(originImgPath));
                img =  BufferedImageHelper.getSource(this.getClass(), originImgPath);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        this.location = new Point((this.getLocationOnScreen().x - root.getLocationOnScreen().x),(this.getLocationOnScreen().y-root.getLocationOnScreen().y));
        bacImg = bacImg.getSubimage(location.x,location.y,width,height);//绘制当前按钮背景为父容器相应位置的背景
        g2d.drawImage(bacImg, 0, 0, width, height, null);
        g2d.drawImage(img, 0, 0, width, height, null);
    }
    public void repaintComponent(boolean paintChangedImg){
        this.paintChangedImg = paintChangedImg;
        this.setIcon(null);
        this.repaint();
    }
}
